//
//  ZYCHomeRootViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/6/25.
//

#import "ZYCHomeRootViewController.h"
#import "ZYCHomeRootHeaderView.h"
#import "ZYCHomeRootParentHeaderView.h"
#import "ZYCHomeRootCell.h"
#import "ZYCWheelBannerModel.h"
#import "ZYCNewBulletinModel.h"
#import "ZYCHomeRootSectionHeaderView.h"
#import "XLPageViewController.h"
#import "ZYCProtalNoticeListViewController.h"
#import "ZYCProtalCalendarViewController.h"
#import "ZYCManageDutyRecordViewController.h"
#import "ZYCManageTourRecordViewController.h"
#import "ZYCManageTourStatisticsViewController.h"
static NSInteger pageSize = 10;

@interface ZYCHomeRootViewController ()<UINavigationControllerDelegate,UITableViewDataSource,UITableViewDelegate,ZYCHomeRootHeaderViewDelegate,ZYCHomeRootSectionHeaderViewDelegate,ZYCHomeRootParentHeaderViewDelegate>

@property (nonatomic, strong) UIScrollView * scrollView;
@property (nonatomic, strong) XLPageViewController* pageViewController;
@property (nonatomic, strong) XLPageViewControllerConfig * config;

@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ZYCHomeRootHeaderView *headerView;
@property (nonatomic, strong) ZYCHomeRootParentHeaderView *parentHeaderView;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, copy) NSString *noticeType;
@end

@implementation ZYCHomeRootViewController
#pragma mark - 生命周期函数
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.title = @"首页";

    [self.view addSubview:self.tableView];
    self.tableView.contentInset = UIEdgeInsetsMake(-kStatusBarHeight, 0, 0, 0);
    self.tableView.contentOffset = CGPointMake(0, kStatusBarHeight);
    NSInteger roleType =  [[NSUserDefaults standardUserDefaults] integerForKey:@"kUserRoleType"];
      if (roleType == 0) {
          self.tableView.tableHeaderView = self.headerView;
      }else{
          self.tableView.tableHeaderView = self.parentHeaderView;
      }

    self.noticeType = @"0";
    // 轮播图
    [self loadBanner];
    // 新闻/公告列表
    [self loadNewBulletinList];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.delegate = self;
};


- (void)loadBanner{
    [[URLManager sharedURLManager] getUrlString:Home_Get_QueryPics requestService:RequestServiceWorkBench parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            if (SUCCESS(responseObject[@"code"])) {
                NSArray *wheelBanner = [NSArray yy_modelArrayWithClass:[ZYCWheelBannerModel class] json:responseObject[@"body"]];
                self.headerView.wheelBannerList = wheelBanner;
            }else{
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
}

- (void)loadNewBulletinList{
    NSDictionary *params = @{
        @"pageSize":@(pageSize),
        @"pageNum":@(self.pageNum),
        @"noticeType":self.noticeType
    };
    [[URLManager sharedURLManager] postUrlString:Home_Post_QueryNoticeList requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            if (SUCCESS(responseObject[@"code"])) {
                NSArray *list = [NSArray yy_modelArrayWithClass:[ZYCNewBulletinModel class] json:responseObject[@"body"][@"list"]];
                [self.dataSource removeAllObjects];
                [self.dataSource addObjectsFromArray:list];
                [self.tableView reloadData];
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
}



#pragma mark - ZYCHomeRootSectionHeaderViewDelegate

- (void)homeRootSectionHeaderViewDidClick:(NSString *)noticeType{
    self.noticeType = noticeType;
    [self loadNewBulletinList];
}

#pragma mark - UINavigationControllerDelegate
-(void) navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:[viewController isKindOfClass:[self class]] animated:YES];
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYCHomeRootCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCHomeRootCell class]) forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(nonnull UITableViewCell *)cell forRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
     // 圆角角度
    CGFloat radius = 7.5f;
    // 设置cell 背景色为透明
    cell.backgroundColor = UIColor.clearColor;
    // 创建两个layer
    CAShapeLayer *normalLayer = [[CAShapeLayer alloc] init];
    CAShapeLayer *selectLayer = [[CAShapeLayer alloc] init];
    // 获取显示区域大小
    CGRect bounds = CGRectInset(cell.bounds, 15, 0);
    // 获取每组行数
    NSInteger rowNum = [tableView numberOfRowsInSection:indexPath.section];
    // 贝塞尔曲线
    UIBezierPath *bezierPath = nil;
    if (rowNum == 1) {
        bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(radius, radius)];
    }else{
        if (indexPath.row == 0) {
            // 每组第一行（添加左上和右上的圆角）
            bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(radius, radius)];
        }else if (indexPath.row == rowNum - 1){
            // 每组最后一行（添加左下和右下的圆角）
            bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(radius, radius)];
        }else{
            // 每组不是首位的行不设置圆角
            bezierPath = [UIBezierPath bezierPathWithRect:bounds];
        }
    }
    // 把已经绘制好的贝塞尔曲线路径赋值给图层，然后图层根据path进行图像渲染render
     normalLayer.path = bezierPath.CGPath;
     selectLayer.path = bezierPath.CGPath;
        
     UIView *nomarBgView = [[UIView alloc] initWithFrame:bounds];
     // 设置填充颜色
//         normalLayer.fillColor = [UIColor colorWithWhite:0.95 alpha:1.0].CGColor;
     normalLayer.fillColor = [[UIColor whiteColor] CGColor];
     // 添加图层到nomarBgView中
     [nomarBgView.layer insertSublayer:normalLayer atIndex:0];
     nomarBgView.backgroundColor = UIColor.clearColor;
//         nomarBgView.backgroundColor = UIColor.whiteColor;
     cell.backgroundView = nomarBgView;
    //此时圆角显示就完成了，但是如果没有取消cell的点击效果，还是会出现一个灰色的长方形的形状，再用上面创建的selectLayer给cell添加一个selectedBackgroundView
     UIView *selectBgView = [[UIView alloc] initWithFrame:bounds];
     selectLayer.fillColor = [[UIColor whiteColor] CGColor];
     [selectBgView.layer insertSublayer:selectLayer atIndex:0];
     selectBgView.backgroundColor = UIColor.clearColor;
     cell.selectedBackgroundView = selectBgView;
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 54.0f;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 75.0f;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}



#pragma mark - getters
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCHomeRootCell class] forCellReuseIdentifier:NSStringFromClass([ZYCHomeRootCell class])];
        _tableView.backgroundColor = HEX_RGB(kBgLightGrayColor);
        _tableView.showsVerticalScrollIndicator = NO;
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
    }
    return _tableView;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSource;
}
- (ZYCHomeRootHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[ZYCHomeRootHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 450)];
//        CGRect viewRect = _headerView.frame;
//        viewRect.size.height = _headerView.viewHeight;
//        viewRect.origin.y = -_headerView.viewHeight;
//        _headerView.frame = viewRect;
        _headerView.delegate = self;
        _headerView.backgroundColor = [UIColor clearColor];
    }
    return _headerView;
}

- (ZYCHomeRootParentHeaderView *)parentHeaderView {
    if (!_parentHeaderView) {
        _parentHeaderView = [[ZYCHomeRootParentHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 365)];
        _parentHeaderView.delegate = self;
        _parentHeaderView.backgroundColor = [UIColor clearColor];
    }
    return _parentHeaderView;
}


#pragma mark - ZYCHomeRootHeaderViewDelegate
//通知
- (void)homeRootHeaderDidClickNotice{
    ZYCProtalNoticeListViewController *vc = [[ZYCProtalNoticeListViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
//周历
- (void)homeRootHeaderDidClickCalendar{
    ZYCProtalCalendarViewController *vc = [ZYCProtalCalendarViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}
//值班记录
- (void)homeRootHeaderDidClickDutyRecord{
    ZYCManageDutyRecordViewController *vc = [ZYCManageDutyRecordViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}
//巡堂记录
- (void)homeRootHeaderDidClickPatrolHallRecord{
    ZYCManageTourRecordViewController *vc = [ZYCManageTourRecordViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}
//巡堂统计
- (void)homeRootHeaderDidClickPatrolHallStatistics{
    ZYCManageTourStatisticsViewController *vc = [ZYCManageTourStatisticsViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}
//听评课
- (void)homeRootHeaderDidClickAttendance{
    
}
//评教评学
- (void)homeRootHeaderDidClickTeachingLearn{
    
}
//就餐统计
- (void)homeRootHeaderDidClickDineStatistics{
    
}


- (void)dealloc{
    self.navigationController.delegate = nil;
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
